/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.ui.form.field.factory;
import com.rcnss.data.Categorie;
import com.rcnss.data.Reclamation;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;
/**
 *
 * @author Guesmi
 */
public class InterventionFieldFactory extends DefaultFieldFactory {

    private IndexedContainer categoriesContainer = new IndexedContainer();
    private boolean enableField = true;
    public InterventionFieldFactory(boolean enabled) {
        super();
        this.enableField = enabled;
    }


    @Override
    public Field createField(Item item, Object propertyId,
                Component uiContext) {
        Field f = super.createField(item, propertyId, uiContext);
        System.out.println("Field ("+propertyId+")");
        ((AbstractField)f).setImmediate(true);
        ((AbstractField)f).setEnabled(enableField);
        if("REC_ID".equals(propertyId))
        {
            TextField tf = (TextField)f;
            tf.setCaption("Reclamation ID");
            tf.setWidth("35em");
            tf.setEnabled(false);
            tf.setNullRepresentation("");
            tf.setInputPrompt("La reclamation");
        } else if("INT_DATE".equals(propertyId))
        {
            TextField tf = (TextField) f;
            tf.setEnabled(false);
            tf.setWidth("35em");
            tf.setNullRepresentation("");
            tf.setCaption("Date");
            tf.setDescription("Ce champs est affecter automatiquement");
        } else if("INT_COMMENT".equals(propertyId))
        {
            TextField tf = (TextField) f;
            tf.setCaption("Message");
            tf.setWidth("35em");
            tf.setRows(10);
            tf.setColumns(35);
            tf.setWordwrap(true);
            tf.setNullRepresentation("");
            tf.setInputPrompt("Votre description du probleme");
        } else if("INT_ID".equals(propertyId))
        {
            TextField tf = (TextField) f;
            tf.setEnabled(false);
            tf.setWidth("35em");
            tf.setCaption("ID");
            tf.setInputPrompt("---------");
            tf.setNullRepresentation("");
            tf.setDescription("Champ generer automatiquement");
        } else if("ETAT".equals(propertyId))
        {
            initCategoriesContainer();
            ComboBox c = new ComboBox("Etat", this.categoriesContainer);
            c.setItemCaptionPropertyId("name");
            c.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            c.setNullSelectionAllowed(false);
            c.setImmediate(true);
            c.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
            c.setWidth("35em");
            c.setInputPrompt("Selectionnez la categorie de la reclamation");
            c.setEnabled(enableField);
            c.addListener(new ValueChangeListener() {

                public void valueChange(ValueChangeEvent event) {
                    //item.getItemProperty("REC_AFF_A").setValue(Categorie.SearchByID(1).);
                }
            });
            return c;
        }
        return f;
    }

    private void initCategoriesContainer()
    {
        this.categoriesContainer.addContainerProperty("name", String.class, null);
        for(int i=0;i<Reclamation.etat.length;i++)
        {
            Item it = this.categoriesContainer.addItem(i);
            if(it!=null)
            {
                it.getItemProperty("name").setValue(Reclamation.etat[i]);
//                if(this.maxLen<s.GetLIB().length())
//                    this.maxLen = s.GetLIB().length();
            }
        }
    }
}